import { PrismaClient } from "@prisma/client";
import { NextApiRequest, NextApiResponse } from "next";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const prisma = new PrismaClient();
  const body = JSON.parse(req.body);

  const skip = Math.max(0, (Number(body.page) - 1) * Number(body.limit));

  const totalPromise = prisma.article.count();
  const listPromise = prisma.article.findMany({
    where: {},
    skip,
    take: body.limit,
    select: {
      title: true,
      id: true,
      user: true,
      tags: true,
    },
  });
  const [total, list] = await Promise.all([totalPromise, listPromise]);
  return res.json({ total, list });
}
